package cn.wolfcode.shop.cloud.web.controller;


import cn.wolfcode.shop.cloud.common.BusinessException;
import cn.wolfcode.shop.cloud.common.Result;
import cn.wolfcode.shop.cloud.domain.OrderInfo;
import cn.wolfcode.shop.cloud.domain.User;
import cn.wolfcode.shop.cloud.mq.MQConstans;
import cn.wolfcode.shop.cloud.mq.OrderMessage;
import cn.wolfcode.shop.cloud.redis.RedisService;
import cn.wolfcode.shop.cloud.redis.SeckillKeyPrefix;
import cn.wolfcode.shop.cloud.service.IOrderInfoService;
import cn.wolfcode.shop.cloud.service.ISeckillGoodService;
import cn.wolfcode.shop.cloud.service.ISeckillOrderService;
import cn.wolfcode.shop.cloud.util.ParamType;
import cn.wolfcode.shop.cloud.util.SeckillGoodsCodeMsg;
import cn.wolfcode.shop.cloud.vo.SeckillGoodVo;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.concurrent.ConcurrentHashMap;


@RestController
@RequestMapping("/order")
public class SeckillOrderInfoController {
    @Autowired
    private ISeckillGoodService seckillService;
    @Autowired
    private ISeckillOrderService seckillOrderService;
    @Autowired
    private IOrderInfoService orderInfoService;
    @Autowired
    private RedisService redisService;
    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    //本地标识
    public static final ConcurrentHashMap<Long,Boolean> isFlag = new ConcurrentHashMap<>();

    @RequestMapping("/{path}/doSeckill")
    public Result<String> doSeckill(Long seckillId, String uuid, @PathVariable String path, @ParamType User user){
        //1 用户是否登录,没有登录提醒用户非法操作
        if(user == null){
            throw new BusinessException(SeckillGoodsCodeMsg.SECKILL_OPT);
        }
        //判断路径是否合法
        String redisPath = redisService.get(SeckillKeyPrefix.SECKILL_PATH, uuid, String.class);
        if (redisPath == null||!path.equals(redisPath)){
            throw new BusinessException(SeckillGoodsCodeMsg.SECKILL_OPT);
        }

        //根据本地标识去进行判断,如果flag为true就停止访问redis
        Boolean flag = isFlag.get(seckillId);
        if (flag!=null&&flag){
            throw new BusinessException(SeckillGoodsCodeMsg.SECKILL_OVER);
        }


        //根据秒杀商品id查询数据,如果查询不到,非法操作
        SeckillGoodVo seckillGoodVo = seckillService.findByIdCache(seckillId);
        if(seckillGoodVo == null){
            throw new BusinessException(SeckillGoodsCodeMsg.SECKILL_OPT);
        }
        //秒杀时间是否在秒杀中,没有就提醒用户
        Date now = new Date();
        if(now.getTime() < seckillGoodVo.getStartDate().getTime() || now.getTime()> seckillGoodVo.getEndDate().getTime() ){
            throw new BusinessException(SeckillGoodsCodeMsg.SECKILL_OPT);
        }
        //利用redis判断用户是否重复下单
        //SeckillOrder seckillOrder = seckillOrderService.selectBySeckillIdAndUserId(seckillId, user.getId());
        boolean exists = redisService.exists(SeckillKeyPrefix.SECKILL_CREATE_ORDER, user.getId() + ":" + seckillId);
        if (exists){
            throw new BusinessException(SeckillGoodsCodeMsg.SECKILL_REPEAT);
        }
        /*//判断库存是否足够
        if (seckillGoodVo.getStockCount()<=0){
            throw new BusinessException(SeckillGoodsCodeMsg.SECKILL_OVER);
        }*/

        //利用redis来控制访问人数
        long count = redisService.decr(SeckillKeyPrefix.SECKILL_STOCK, seckillId + "");
        if (count<0){
            isFlag.put(seckillId, true);
            throw new BusinessException(SeckillGoodsCodeMsg.SECKILL_OVER);
        }

        //秒杀
        //封装消息
        OrderMessage orderMessage = new OrderMessage();
        orderMessage.setSeckillId(seckillId);
        orderMessage.setUserId(user.getId());
        orderMessage.setUuid(uuid);
        rocketMQTemplate.syncSend(MQConstans.CREATE_ORDER_DEST, orderMessage);
        //String orderNo = orderInfoService.seckill(seckillId, user.getId());
        return Result.success("正在抢购中......");

    }

    @RequestMapping("/getOrderInfoByOrderNo")
    public Result<OrderInfo> getOrderInfoByOrderNo(String orderNo, @ParamType User user){
        if(user == null){
            throw new BusinessException(SeckillGoodsCodeMsg.SECKILL_OPT);
        }

        OrderInfo orderInfo = orderInfoService.getOrderInfoByOrderNo(orderNo);
        if (orderInfo == null){
            throw new BusinessException(SeckillGoodsCodeMsg.SECKILL_OPT);
        }

        //只有订单用户才能看到
        if (!orderInfo.getUserId().equals(user.getId())){
            throw new BusinessException(SeckillGoodsCodeMsg.SECKILL_OPT);
        }
        return Result.success(orderInfo);
    }

}
